package my.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class ConvertUtil {
	
	private static Logger log = Logger.getLogger(ConvertUtil.class);

	/**
	 * 转Long
	 * @param obj
	 */
	public static Long toLong(Object obj) {
		Long l = null;
		
		try {
			l = Long.class.cast(obj);
		} catch (Exception e) {
			l = Long.valueOf(obj.toString());
		}
		
		return l;
	}
	
	/**
	 * 转String
	 * @param obj
	 */
	public static String toString(Object obj) {
		String str = null;
		
		try {
			str = String.class.cast(obj);
		} catch (Exception e) {
			str = String.valueOf(obj);
		}
		
		return str;
	}
	
	/**
	 * 毫秒转秒
	 * @param millis
	 */
	public static String millisToSecond(Integer millis) {
		Float second = null;
		
		try {
			second = Float.class.cast(millis);
		} catch (Exception e) {
			second = Float.valueOf(millis.toString());
		}
		
		return String.format("%.3f", (second/1000));
	}
	
	/**
	 * 转成英文标点
	 * @param content
	 */
	public static String toEnPunctuation(String content) {
		String[] regs = { "！", "，", "。", "；", "：", "!", ",", ".", ";", ":" };
		
		for ( int i = 0; i < regs.length / 2; i++ )
		{
			content = content.replaceAll(regs[i], regs[i + regs.length / 2]);
		}
		
		return content;
	}
	
	/**
	 * 去掉标点
	 * @param content
	 */
	public static String toTrimPunctuation(String content) {
		String[] regs = { "！", "[，]", "。", "；", "：", "[？]", "!", "[,]", "[.]", "[;]", "[:]", "[?]" };
		
		for ( int i = 0; i < regs.length; i++ )
		{
			content = content.replaceAll(regs[i], "");
		}
		
		return content;
	}
	
	/**
	 * 转成小写
	 * @param content
	 */
	public static String toLowerCase(String content) {
		content = StringUtils.lowerCase(content);
		
		return content;
	}
	
	/**
	 * 去除空格
	 * @param content
	 */
	public static String toTrim(String content) {
		content = StringUtils.trim(content);
		
		return content;
	}
	
	/**
	 * 分钟转小时
	 * @param millis
	 */
	public static String minuteToHour(Integer minute) {
		Integer hour = null;
		Integer minuteInt = null;
		String hourMinuteStr = null;
		
		try {
			hour = minute / 60;
			minuteInt = minute % 60;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(minute > 60) {
			hourMinuteStr = hour + " 小时 " + minuteInt + " 分钟";
		}
		else if(minute == 60) {
			hourMinuteStr = "1 小时  ";
		}
		else {
			hourMinuteStr = minuteInt + " 分钟";
		}
		
		return hourMinuteStr;
	}
	
	public static String[] toStringArray(String... strings) {
		return strings;
	}
	
	@SuppressWarnings("unchecked")
	public static Class[] toClassArray(Class... classes) {
		return classes;
	}
	
	public static Object[] toObjectArray(Object... objects) {
		return objects;
	}
	
	public static <T> T[] toNoRepeatArray(T[] array) {
		Set<T> set = new LinkedHashSet<T>(Arrays.asList(array));
		T[] noRepeatArray = Arrays.copyOf(array, set.size());
		
		return set.toArray(noRepeatArray);
	}
	
	public static Map<String, Object> toMap(String[] keys, Object[] values) {
		Map<String, Object> map = null;
		
		if(keys != null && values != null) {
			if(keys.length == values.length) {
				if (keys.length > 0 && values.length > 0) {
					map = new HashMap<String, Object>();
					for (int i = 0; i < keys.length; i++) {
						String key = keys[i];
						Object value = values[i];
						
						map.put(key, value);
					}
				} else {
					log.debug("键字段个数或值字段个数小于0");
				}
			} else {
				log.debug("键字段个数和值字段个数不匹配");
			}
		}
		
		return map;
	}
	
}
